package com.power.sifa.correction.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.plf.core.base.model.Ha_attachment;
import com.plf.core.base.service.Ha_attachmentService;
import com.plf.core.common.service.BaseServiceImpl;
import com.power.sifa.correction.model.Crc_person;
import com.power.sifa.statistics.model.vo.NightRuleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.power.sifa.correction.model.Cre_warning_detail;
import com.power.sifa.correction.dao.Cre_warning_detailDao;
import com.power.sifa.correction.service.Cre_warning_detailService;
import com.power.sifa.correction.model.pageInfo.Cre_warning_detailPageInfo;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 *  预警信息违规详情业务层实现
 */
@Service
public class Cre_warning_detailServiceImpl extends BaseServiceImpl implements Cre_warning_detailService {

    @Resource
    private Cre_warning_detailDao dao;
    @Resource
    private Ha_attachmentService ha_attachmentService;

    @Override
    public Cre_warning_detail getById(Serializable id) {
        return dao.findById(id).orElse(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(Cre_warning_detail cre_warning_detail) {
    	// save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (cre_warning_detail.getPrimaryKey() != null) {
        	 Cre_warning_detail cre_warning_detailfDb = dao.findById(cre_warning_detail.getPrimaryKey()).orElse(null);
             if(cre_warning_detailfDb != null) {
                BeanUtils.copyProperties(cre_warning_detail, cre_warning_detailfDb);
                dao.save(cre_warning_detailfDb);
                return;
             }
        }
        dao.save(cre_warning_detail);
    }

    @Override
    public void removeById(Serializable id) {
        dao.deleteById(id);
    }

    @Override
    public Page<Cre_warning_detail> findAll(Cre_warning_detailPageInfo pageInfo) {
        //排序等定义
        Sort sort;
        if("asc".equals(pageInfo.getOrder())) {
            sort = Sort.by(Sort.Direction.ASC, pageInfo.getSort());
        }else {
            sort = Sort.by(Sort.Direction.DESC, pageInfo.getSort());
        }
        //分页的定义
        Pageable pageable = PageRequest.of(pageInfo.getPageNumber()<=1 ? 0 : pageInfo.getPageNumber()-1, pageInfo.getPageSize(), sort);
        //查询条件
        Specification spec = this.createSpecification(pageInfo);
        return dao.findAll(spec, pageable);
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(List<Integer> ids) {
        dao.removeByIds(ids);
    }

    @Override
	public List<Cre_warning_detail> findAll() {
		return dao.findAll();
	}

	@Override
	public void saveAll(List<Cre_warning_detail> list) {
		dao.saveAll(list);
	}

    @Override
    public List<Cre_warning_detail> findByWarningId(String warningId) {
        return dao.findByWarningId(warningId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Map<String, Object>> queryListByWarnid(String warnid) {
        List<Map<String, Object>> list = this.dao.queryListByWarnid(warnid);
        if(!CollectionUtils.isEmpty(list)) {
            // 拿取对应的id
            List<String> attachmentIds = list.stream().filter(element -> ObjectUtil.isNotNull(element) && ObjectUtil.isNotNull(element.get("photo"))).map(v -> (String) v.get("photo")).collect(Collectors.toList());
            Map<String, Ha_attachment> ha_attachmentMap = ha_attachmentService.getHa_attachmentsByIds(attachmentIds);
            if (ObjectUtil.isNotNull(ha_attachmentMap)) {
                for(int i=0; i<=list.size()-1; i++) {
                    list.set(i, new HashMap<>(list.get(i)));
                    if(ha_attachmentMap.containsKey(list.get(i).get("photo"))) {
                        list.get(i).put("photo", (ha_attachmentMap.get(list.get(i).get("photo"))).getPath());
                    }
                }
            }
        }
        return list;
    }

    @Override
    public NightRuleVO getNightRuleVO(int nightout, Integer gaid) {
        return dao.getNightRuleVO(nightout, gaid);
    }
}
